<template>
  <div>
    <div class="dialogPop">
      <el-dialog :visible.sync="addData.dialogAdd" :before-close="handleClose" size="tiny">
        <p class="dialog-title">{{addData.popTitle}}</p>
        <el-form :model="addData" :rules="rulesAdd" ref="addData" label-width="124px">
                <el-row :gutter="20">
                    <el-col :span="20">
                    <el-form-item label="字典编号：">
                        <el-input v-model="addData.wordNum" :placeholder="addData.autoWarn" :disabled="addData.isDisabled"></el-input>
                    </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="20">
                    <el-form-item label="字典名称：" prop="wordName">
                        <el-input v-model="addData.wordName" placeholder="字典名称"></el-input>
                    </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="20">
                    <el-form-item label="字典描述：">
                        <el-input v-model="addData.wordDetail" placeholder="字典描述"></el-input>
                    </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="20">
                    <el-form-item label="字典类型：">
                        {{addData.wordType|filterType}}
                    </el-form-item>
                    </el-col>
                </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <p class="btn">
            <el-button type="primary" @click="addInfor('addData')">确 认</el-button>
            <el-button @click="resetFormdelDialogAdd('addData')">取 消</el-button>
          </p>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { addDict, updateDict} from 'api/system';
export default {
    props: {
        addData: {
            default() {
                return {
                    dialogAdd: false // 默认不显示
                }
            },
            required: true
        }
    },
    filters: {
        filterType(val) {
            // return ['', '学科', '学段', '关系', '学制', '年级', '户口', '职称', '证件类型', '举报类型'][val];
            return ['', '科目字典'][val];
        }
    },
    data() {
        return {
            rulesAdd: {
                wordName: [{ required: true, message: '字典名称', trigger: 'blur' }]
            }

        }
    },
    methods: {
        // 点击弹窗的确认按钮，添加和修改信息
        addInfor(addData) {
            this.$refs[addData].validate((valid) => {
                if (valid) {// 验证通过
                    // 如果是添加弹窗
                    let desc=this.addData.wordDetail;
                    let name=this.addData.wordName;
                    if (this.addData.status==1) {
                        addDict(desc, name).then(res => {
                            this.$emit('searchList');
                            if (res.data.status==200) {
                                this.$message({
                                    type: 'success',
                                    message: '添加成功!'
                                });
                                this.handleClose();
                                this.$emit('searchList');
                            } else {
                                let warnMessage=res.data.errorMsg;
                                this.$message({
                                    type: 'success',
                                    message: warnMessage
                                });
                            }
                        })
                    }
                    // 如果是修改弹窗
                    if (this.addData.status==2) {
                        let id=this.addData.wordNum;
                        updateDict(desc, name, id).then(res => {
                            this.$emit('searchList');
                            if (res.data.status==200) {
                                this.$message({
                                    type: 'success',
                                    message: '修改成功!'
                                });
                                this.handleClose();
                                this.$emit('searchList');
                            } else {
                                let warnMessage=res.data.errorMsg;
                                this.$message({
                                    type: 'success',
                                    message: warnMessage
                                });
                            }
                        });
                    }
                } 
            });
        },
       // 弹出框的关闭按钮
        handleClose() {
            this.$refs.addData.resetFields();
            this.delDialogAdd();
        },
        resetFormdelDialogAdd(addData) {
            this.$refs[addData].resetFields();
            this.delDialogAdd();
        },
        // 还原弹窗信息（addDate是接受的父组件，不可以addDate={}这样修改）
        delDialogAdd() {
            this.addData.dialogAdd=false;
            this.addData.wordNum='';
            this.addData.wordName='';
            this.addData.wordDetail='';
            // this.addData.wordType='';
        }
    }
}
</script>
<style lang="scss" scoped>
@import '../../style/dialog.scss';
</style>

